package com.zhihuishu.micro.course.service.impl;

import com.zhihuishu.micro.course.dao.LessonVideoMapper;
import com.zhihuishu.micro.course.dto.LessonVideoDto;
import com.zhihuishu.micro.course.model.Lesson;
import com.zhihuishu.micro.course.model.LessonVideo;
import com.zhihuishu.micro.course.model.LessonVideoCount;
import com.zhihuishu.micro.course.service.LessonVideoTreenityDBService;
import com.zhihuishu.toolkit.helper.BeanHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service("lessonVideoAllServiceDB")
public class LessonVideoAllServiceDBImpl extends BaseTreenityBatchServiceDBImpl<LessonVideoDto, LessonVideo> implements LessonVideoTreenityDBService {

    @Resource
    private LessonVideoMapper lessonVideoMapper;

    @Override
    public LessonVideoCount countLvsAndGetLessonId(long lvsId) {
        LessonVideoCount chapter = lessonVideoMapper.countLvsAndGetLessonId(lvsId);
        return chapter;
    }

    @Override
    public LessonVideo selectFirstVideoInfo(long lessonId) {
        LessonVideo chapter = lessonVideoMapper.selectFirstVideoInfo(lessonId);
        return chapter;
    }

    @Override
    public LessonVideoDto detail(Integer lessonId) {
        LessonVideo chapter = lessonVideoMapper.get(lessonId);
        LessonVideoDto chapterDto = toDto(chapter);
        return chapterDto;
    }

    @Override
    public void update(LessonVideoDto chapterDto, long updateUserId) {
        LessonVideo model = toModel(chapterDto);
        lessonVideoMapper.update(model);
    }

    @Override
    public void delete(Integer lessonId, long deleteUserId) {
        lessonVideoMapper.remove(lessonId);
    }

    @Override
    public void sort(List<LessonVideoDto> dbDtoList, long updateUserId) {
        List<LessonVideo> modelList = new LinkedList<LessonVideo>();
        for (LessonVideoDto chapterDto : dbDtoList) {
            modelList.add(toModel(chapterDto));
        }
        lessonVideoMapper.sort(modelList);
    }

    @Override
    public Integer create(LessonVideoDto chapter, long creator) {
        LessonVideo model = toModel(chapter);
        model.setIsDeleted(0);
        lessonVideoMapper.insertSelective(model);

        Integer lessonId = model != null ? model.getId() : null;
        chapter.setId(lessonId);

        return lessonId;
    }

    public LessonVideoDto toDto(LessonVideo model) {
        if(model == null) return null ;
        LessonVideoDto dto = new LessonVideoDto() ;
        BeanHelper.copyProperties(model,dto);

//        dto.setId(model.getId());
//        dto.setLessonId(model.getLessonId());
//        dto.setName(model.getName());
        dto.setRank(model.getSort());
//        dto.setVideoId(model.getVideoId());
//        dto.setVideoSec(model.getVideoSec());
//        dto.setVideoImg(model.getVideoImg());
        return dto;
    }


    public LessonVideo toModel(LessonVideoDto dto) {
        if(dto == null) return null ;
        LessonVideo model = new LessonVideo() ;
        BeanHelper.copyProperties(dto,model);
//        model.setId(dto.getId());
//        model.setChapterId(dto.getChapterId());
//        model.setLessonId(dto.getLessonId());
//        model.setName(dto.getName());
        model.setSort(dto.getRank());
//        model.setVideoId(dto.getVideoId());
//        model.setVideoImg(dto.getVideoImg());
        return model ;
    }



    @Override
    public List<Integer> listDtoIds(long courseId) {
        List<Integer> lessonIds = lessonVideoMapper.listByCourseId((int) courseId);
        return lessonIds;
    }

    @Override
    public List<LessonVideoDto> list(List<Integer> listLessonIds) {
        final List<LessonVideoDto> chapters = new ArrayList<LessonVideoDto>();
        if (!CollectionUtils.isEmpty(listLessonIds)) {
            List<LessonVideo> list = lessonVideoMapper.getList(listLessonIds);
            for (LessonVideo lessonVideo : list) {
                chapters.add(toDto(lessonVideo));
            }
        }
        return chapters;
    }

    @Override
    public void deleteMulti(List<Integer> dtoIds, long deleteUserId) {

    }
}
